CLS = tongjithesis.cls
BIB = ref.bib
THESIS = thesis
MAIN = main.tex
ACK = ack.tex
ABS = abstract.tex
FIG = figures

pdf: $(THESIS).pdf

$(THESIS).pdf: $(THESIS).tex $(BIB) $(CLS) $(ACK) $(ABS) $(FIG)
	@latexmk -xelatex $<

$(THESIS).tex: $(THESIS).md $(MAIN)
	@pandoc -s --template=${MAIN} --top-level-division=chapter --listings $< -o $@

%.tex: %.md
	@pandoc --top-level-division=chapter --listings $< -o $@

wordcount : $(THESIS).tex
	@texcount $< -inc -chinese-only | awk '/total/ {getline; print "中文字符\t\t\t:",$$4}'
	@texcount $< -inc -chinese | awk '/total/ {getline; print "总字数（英文单词 + 中文字符）\t:",$$4}'

clean:
	-@latexmk -c -silent $(THESIS).tex 2> /dev/null
	-@rm -f *.thm *.run.xml *.bbl *.xdv 2> /dev/null
	-@rm -f $(THESIS).tex $(ACK) 2> /dev/null

cleanall:
	-@latexmk -CA -silent $(THESIS).tex 2> /dev/null
	-@rm -f *.thm *.run.xml *.bbl 2> /dev/null
	-@rm -f $(THESIS).tex $(ACK) 2> /dev/null

.PHONY: pdf clean